/**
 * PerformHashFuction.java of project FileHashVerification created on Date: 03-Oct-2011
 * veritas
 */
package fileHashVerification.file.hash;

/**
 * @author veritas
 *
 */
import java.io.*;
import java.security.MessageDigest ;

public class PerformHashFuction implements HashFunction{

	/* (non-Javadoc)
	 * @see fileHashVerification.file.hash.HashFunction#verifyHashCode(java.lang.String, java.lang.String)
	 */
	@Override
	public boolean verifyHashCode(String beforeHashCode , String afterHashCode) throws Exception {
            if(beforeHashCode.equals(afterHashCode))
                return true;
            else 
		return false;
	}

	/* (non-Javadoc)
	 * @see fileHashVerification.file.hash.HashFunction#createChecksum(java.lang.String)
	 */
	@Override
	public byte[] createChecksum(String filename) throws Exception {
        
            File file = new File(filename);
            InputStream fis =  new FileInputStream(file);
         
            byte[] buffer = new byte[1024];
            MessageDigest complete = MessageDigest.getInstance("SHA1");
	    
            int numRead;
	    do {
	    	numRead = fis.read(buffer);
	    	if (numRead > 0) {
	    		complete.update(buffer, 0, numRead);
	    	}
	    } while (numRead != -1);
	     fis.close();
	     return complete.digest();
	   }
	/* (non-Javadoc)
	 * @see fileHashVerification.file.hash.HashFunction#getSHA1Checksum(java.lang.String)
	 */
	@Override
	public String getSHA1Checksum(String filename) throws Exception {
            byte[] b = createChecksum(filename);
            String result = "";
	    for (int i=0; i < b.length; i++) {
	    	result +=
	        Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
	    }
            return result;
	}
}